package com.example.xymarket.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xymarket.activity.OrderDetailActivity;
import com.example.xymarket.common.MyBitMap;
import com.example.xymarket.common.MyUrl;
import com.example.xymarket.httpUtil.AsyncImageLoader;
import com.example.xymarket.R;
import com.example.xymarket.po.Order;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import android.os.Handler;

public class OrderAdapter extends BaseAdapter {

    private List<Order> items;
    private Context context;
    private Handler parentHandler;
    private int layout= R.layout.item_order;
    public HashMap<Integer, Drawable> hm;

    public OrderAdapter(List<Order> items, Context context, Handler parentHandler) {
        this.items = items;
        this.context = context;
        this.parentHandler = parentHandler;
        hm=new HashMap<>();
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            Order order = items.get(position);
            String url = MyUrl.ImagesUrl + order.getGoods().getImage();
            OrderAdapter.MyViewHolder viewHolder = new OrderAdapter.MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(layout, null);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Gson gson=new Gson();
                    String orderJson=gson.toJson(order);
                    Intent intent=new Intent(context,OrderDetailActivity.class);
                    intent.putExtra("order",orderJson);
                    byte[] bitmap= MyBitMap.bitmapToBytes(MyBitMap.drawableToBitmap2(viewHolder.imageView.getDrawable()));
                    intent.putExtra("bitmap",bitmap);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(intent);
                }
            });
            // 初始化 MyViewHolder 方便重用
            viewHolder.id = order.getId();
            viewHolder.name = (TextView) convertView.findViewById(R.id.order_name);
            viewHolder.name.setText(order.getGoods().getTitle());
            viewHolder.price = (TextView) convertView.findViewById(R.id.order_price);
            viewHolder.price.setText(String.valueOf(order.getTotalPrice()/ 100) + "." + String.valueOf(order.getTotalPrice() % 100));
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.order_avatar);
            if(hm.get(position)!=null)
            {
                viewHolder.imageView.setImageDrawable(hm.get(position));
            }
            else
            {
                AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
                asyncImageLoader.loadImage(url, viewHolder.imageView,hm,position);
            }
            convertView.setTag(viewHolder);
            return convertView;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public final class MyViewHolder {
        int id;
        TextView name;
        TextView price;
        ImageView imageView;
    }
}
